<?php

class CustomerController extends \BaseController {

	/**
	 * Display a listing of the resource.
	 *
	 * @return Response
	 */
	public function index()
	{
        // get all the customers
        //$customers = Customer::all();

        // load the view and pass the customers
        return View::make('customers.index');
	}

	/**
	 * Show the form for creating a new resource.
	 *
	 * @return Response
	 */
	public function create()
	{
		//Create New Customer. Load /customers/create.blade.php
        return View::make('customers.create');
	}

    /**
     * Get customers from database and return json formatted for the datatable plugin
     * @return Datatable view(json)
     */
    public  function Datatable(){
        $customers = Customer::select(array('customers.id','customers.name','customers.billing_adr','customers.shipping_adr','customers.credit_card','customers.contact'));
        return Datatables::of($customers)->add_column('operations','<a href="{{ URL::to( \'customers\', array( $id,\'edit\' )) }}">Edit</a>

                {{ HTML::link("customers/$id", "Delete", array( "data-method" => "delete")) }}


            ')->make();
    }

    public function Report(){
        return View::make('customers.create');
    }

	/**
	 * Store a newly created resource in storage.
	 *
	 * @return Response
	 */
	public function store()
    {
        // validate
        $rules = array(
            'name'       => 'required',
        );
        $validator = Validator::make(Input::all(), $rules);

        // process the login
        if ($validator->fails()) {
            return Redirect::to('customers/create')
                ->withErrors($validator)
                ->withInput(Input::except('password'));
        } else {
            // store
            $customer = new Customer();
            $customer->name              = Input::get('name');
            $customer->billing_adr       = Input::get('billing_adr');
            $customer->shipping_adr      = Input::get('shipping_adr');
            $customer->contact           = Input::get('contact');
            $customer->credit_card       = Input::get('credit_card');
            $customer->save();

            // redirect
            Session::flash('message', 'Successfully created customer!');
            return Redirect::to('customers');
        }
    }

	/**
	 * Display the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
		//get the customer
        $customer = Customer::find($id);

        //show the view and pass the custoemr id
        return View::make('customers.show')->with('customer', $customer);
	}

	/**
	 * Show the form for editing the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
		//get the customer id to edit
        $customer = Customer::find($id);

        //show the edit view form
        return View::make('customers.edit')->with('customer',$customer);

	}

	/**
	 * Update the specified resource in storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id)
	{
        // validate
        // read more on validation at http://laravel.com/docs/validation
        $rules = array(
            'name'       => 'required'
        );
        $validator = Validator::make(Input::all(), $rules);

        // process the login
        if ($validator->fails()) {
            return Redirect::to('customers/' . $id . '/edit')
                ->withErrors($validator)
                ->withInput(Input::except('password'));
        } else {
            // store
            $customer = Customer::find($id);
            $customer->name       = Input::get('name');
            $customer->save();

            // redirect
            Session::flash('message', 'Successfully updated Customer!');
            return Redirect::to('customers');
        }
	}

	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
		//delete the customer with id
        $customer = Customer::find($id);
        $customer->delete();

        //redirect
        Session::flash('message','Successfully deleted the customer');
        return Redirect::to('customers');
	}

}